﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ThiTracNghiem
{
    public partial class FormDeviceMgr : Form
    {
        public FormDeviceMgr()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void FormDeviceMgr_Load(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(Program.ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT * FROM sys.backup_devices", con))
                {
                    con.Open();
                    SqlDataReader reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        DataGridViewRow row = new DataGridViewRow();
                        row.CreateCells(dgvDevices);
                        row.Cells[0].Value = reader[0].ToString();
                        row.Cells[1].Value = reader[3].ToString();
                        dgvDevices.Rows.Add(row);
                    }
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FormNewDevice frmNewDivice = new FormNewDevice();
            if (frmNewDivice.ShowDialog() == DialogResult.OK)
            {
                String name = frmNewDivice.txtTen.Text;
                String location = frmNewDivice.txtFileLocation.Text;
                frmNewDivice.Dispose();

                using (SqlConnection con = new SqlConnection(Program.ConnectionString))
                {
                    using (SqlCommand cmd = new SqlCommand("master.dbo.sp_addumpdevice", con))
                    {
                        try
                        {
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Parameters.Add("@devtype", SqlDbType.NVarChar).Value = "DISK";
                            cmd.Parameters.Add("@logicalname", SqlDbType.NVarChar).Value = name;
                            cmd.Parameters.Add("@physicalname", SqlDbType.NVarChar).Value = location;
                            con.Open();
                            cmd.ExecuteNonQuery();
                            MessageBox.Show("Thực hiện tạo device thành công!", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            DataGridViewRow row = new DataGridViewRow();
                            row.CreateCells(dgvDevices);
                            row.Cells[0].Value = name;
                            row.Cells[1].Value = location;
                            dgvDevices.Rows.Add(row);
                        }
                        catch (System.Exception ex)
                        {
                            MessageBox.Show("Có lỗi đã xảy ra.\nChi tiết: " + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (dgvDevices.SelectedRows.Count > 0)
            {
                if (MessageBox.Show("Bạn có chắc chắn muốn xóa device này?\nXóa device sẽ đồng thời xóa tất cả bản backup có trên device đó.", "Xác nhận", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
                {
                    String name = dgvDevices.SelectedRows[0].Cells[0].Value.ToString();
                    using (SqlConnection con = new SqlConnection(Program.ConnectionString))
                    {
                        using (SqlCommand cmd = new SqlCommand("master.dbo.sp_dropdevice", con))
                        {
                            try
                            {
                                cmd.CommandType = CommandType.StoredProcedure;
                                cmd.Parameters.Add("@logicalname", SqlDbType.NVarChar).Value = name;
                                con.Open();
                                cmd.ExecuteNonQuery();
                                MessageBox.Show("Thực hiện xóa device thành công!", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                dgvDevices.Rows.Remove(dgvDevices.SelectedRows[0]);
                            }
                            catch (System.Exception ex)
                            {
                                MessageBox.Show("Có lỗi đã xảy ra.\nChi tiết: " + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                }
            }
        }
    }
}
